package com.manager.car.dao;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.orm.hibernate5.HibernateCallback;

import com.manager.car.po.MessageInfo;


public class MessageDaoImpl extends BaseDao<MessageInfo> implements IMessageDao {
    
	
	//查询分页
	
	public List<MessageInfo> queryAll(final String name, final int limit, final int page) {
		@SuppressWarnings("unchecked")
		List<MessageInfo> list=(List<MessageInfo>) this.getHibernateTemplate().execute(new HibernateCallback<Object>() {

			
			public Object doInHibernate(Session session) throws HibernateException {
				Query<MessageInfo> query=null;
				//查询全部留言
                if(name==null || name.equals("")) {
                	query=session.createQuery(" from MessageInfo where 1=1  order by msgId desc ");
				}
				//未屏蔽
                else if(name=="s" ) {
				 query=session.createQuery(" from MessageInfo where msgStatic=0 order by msgId desc ");
				}
				//已屏蔽
				else if(name=="W") {
					query=session.createQuery(" from MessageInfo where msgStatic=1 order by msgId desc ");
				}
				else if(name.substring(name.length()-1,name.length()).equals("s")) {
					query=session.createQuery(" from MessageInfo where 1=1 and msgContent like ? order by msgId desc ").setParameter(0,"%"+ name.substring(0,name.length()-1)+"%");
				}
				else if(name.substring(name.length()-1,name.length()).equals("w")) {
					query=session.createQuery(" from MessageInfo where msgStatic=1 and msgContent like '%?%' order by msgId desc ").setParameter(1, name.substring(name.length()-1));
				}
				int begin=(page-1)*limit;
				query.setFirstResult(begin);
				query.setMaxResults(limit);
				return query.list();
			}
		});
		
		return list;
	}

//	public int getcount(String name, int limit, int page) {
//		// TODO Auto-generated method stub
//		return 0;
//	}
public int getcount( final String name,final int limit,final int page) {
		
		int count= (Integer) this.getHibernateTemplate().execute(new HibernateCallback<Object>() {

			public Object doInHibernate(Session session) throws HibernateException {
				Query<MessageInfo> query=null;
				//查询全部留言
                if(name==null || name.equals("")) {
                	query=session.createQuery(" from MessageInfo where 1=1 order by msgId desc ");
				}
				//未屏蔽
                else if(name=="s" ) {
				 query=session.createQuery(" from MessageInfo where msgStatic=0 order by msgId desc ");
				}
				//已屏蔽
				else if(name=="W") {
					query=session.createQuery(" from MessageInfo where msgStatic=1 order by msgId desc ");
				}
				else if(name.substring(name.length()-1,name.length()).equals("s")) {
					query=session.createQuery(" from MessageInfo where 1=1 and msgContent like ? order by msgId desc ").setParameter(0, "%"+ name.substring(0,name.length()-1)+"%");
				}
				else if(name.substring(name.length()-1,name.length())=="w") {
					query=session.createQuery(" from MessageInfo where msgStatic=1 and msgContent like '%?%' order by msgId desc ").setParameter(1, name.substring(name.length()-1));
				}

				return query.list().size();
			}
		});
		return count;
	}

	

	
	

}
